/*
 * @author:joymin
 * @说明：工情模块的js
 */
/*******************************************************************
                       页面公用函数
 *******************************************************************/


$(function() {
	loadExtentFile("../js/systemManage/jquery.bigautocomplete.js","js");
	loadExtentFile("../js/systemManage/jquery.validationEngine.js","js");
})
function loadExtentFile(filename, fileType){
    if(fileType == "js"){
        var oJs = document.createElement('script');        
        oJs.setAttribute("type","text/javascript");
        oJs.setAttribute("src", filename);//文件的地址 ,可为绝对及相对路径
        document.getElementsByTagName("head")[0].appendChild(oJs);//绑定
    }else if(fileType == "css"){
        var oCss = document.createElement("link"); 
        oCss.setAttribute("rel", "stylesheet"); 
        oCss.setAttribute("type", "text/css");  
        oCss.setAttribute("href", filename);
        document.getElementsByTagName("head")[0].appendChild(oCss);//绑定
    }
}
function getUserRole(){
	
	$.ajax({    			
	    url:'GetUserRole.action',
		type:'POST',
		data:{},
		dataType:'json',
		success:function(data){			
			//如果不是管理站用户
			if(data=='role_r2'){
				/*$("#saveBtn").attr("disabled","disabled");*/
				$("#saveBtn").show();
				$("#submitBtn").show();
				$("#addBtn").show();
				$("#updateBtn").show();
				$("#delBtn").show();
				$("#list-name-input").css("margin-top","0px");				
			}
        },
        error: function() {
            //请求出错处理
        	//alert("error");
        	ui.failed("连接不上服务器，请稍后");
        }
        	
  });
}




//action1取初始化数据,axtion2根据单个code取单条记录
function getInitData(action1,action2,name){
	//alert("getInitData");
	$.ajax({
			//url:'GetReserviorName.action?personCode='+personCode,
		    url:action1,
			type:'POST',
			data:'{}',
			dataType:'json',
			success:function(data)
			{
				//alert("getInitData success");
				//返回的数据前len-1个是名字跟编号,最后一个是最新的一条记录的数据
				if(null!=data){
					//dealInitData(data,"resName");
					dealInitData(data,action2,name);
					if(name=='irrName'){
						/*jq("#addBtn").hide();
						$("#list-name-input").css("margin-top","-18px");*/
						$("#addBtn").css("visibility","hidden")
					}
				}					
			}
	 });
}

//根据编号获取单个记录的全部数据
function getRecordByCode(action,code){	
	$.ajax({			
		    url:action,
			type:'POST',
			data:{'code':code},
			dataType:'json',
			success:function(data)
			{							
				//返回的数据resCode对应的水库记录数据
				if(null!=data){
					writeForm(data);					
				}					
			}
	 });
}

//提交表单事件，form为表单id，action为后台处理函数，flag为按钮类别
function submitForm(form,action){
	var bool=jQuery('#'+form).validationEngine('validate');	
	if(bool==false){		
		ui.failed("请按正确格式填写！");
		return;	
	}
	else{
		var waterPlantCode=$("#waterPlantCode").val();
		//var personCode=jq(parent.personCode).text();
	   // personCode=personCode.substring(3,personCode.length);
		setIsDisable("",false);
	    var msg="是否提交?";
	    var dag="save"+form+"dialog";
	    document.getElementById(form).action=action+"?waterPlantCode="+waterPlantCode+"";
		/*switch(flag){
		case 1: //提交按钮
			msg="是否提交？"
		    document.getElementById(form).action=action+"?btnFlag=1";
			break;
		case 2: //保存按钮
			msg="是否保存？"
		    document.getElementById(form).action=action+"?btnFlag=2";			
			break;
		}*/
	    ui.confirm({
	        tip:msg,
	        dClass:dag,
	        yes:function(){
	        	closeDialog($("."+dag));
	        	document.getElementById(form).submit();        	
	        }
	    });
	}		
}

//添加按钮事件
function addBtnFun(form,name){
	setIsDisable(name,false);
	$("#"+name).hide();
	/*jq("#list-name-input").hide();
	jq("#nameCopy").show();
	jq(obj).css("margin-left","0px");*/
}

//将单条记录填充到form表单
function writeForm(data){	
	for(var key in data){		
		if(key.indexOf("Name")>0){
			continue;
		}
		$('#'+key).val($.trim(data[key]));
	}
}

//处理初始化信息
function dealInitData(data,action,name){
	var jsonData = eval("(" + data + ")");
	if(jsonData.length>1){
		var opt="";
		for(var i=0;i<jsonData.length-1;++i){
			opt+='<option value="'+jsonData[i].id+'">'+jsonData[i].name+'</option>';
		}
		$('#'+name).html(opt);
		$('#saveInf').hide();
	}
	else{
		$('#'+name).hide();
		$('#saveInf').show();
		//jq('#saveInf').css("visibility","visible");
	}	
	for(var key in jsonData[jsonData.length-1]){
		var value=$.trim(jsonData[jsonData.length-1][key]);
		if(value==0){
			$('#'+key).val("");
		}
		else{
			$('#'+key).val(value);
		}
		//将名字放入到文本框中
		if(key==name){
			$('#list-name-for-select').val(value);
		}
	}	
	nameInputFocus(action,name);
}


function setIsDisable(name,flag){	
	var inputs=$("#inquire1 input[type='text']");	
	for (var i = 0;i<inputs.length; i++){
		$(inputs[i]).attr("disabled",flag);
	}	
	var textarea=$("#inquire1 textarea");	
	for (var i = 0;i<textarea.length; i++){
		$(textarea[i]).attr("disabled",flag);
	}
	var selects=$("#inquire1 select");	
	for (var i = 0;i<selects.length; i++){
		$(selects[i]).attr("disabled",flag);
	}
	if(flag==true){
		$('#'+name).attr("disabled",false);
		$('#list-name-for-select').attr("disabled",false);
		//$('.form-control').attr("disabled",false);
	}
	
}

function nameInputFocus(action,name){
	//alert($("#"+name).length);
	var jsonStr="[{title:";	
	$("#"+name).find('option').each(function(){		    
	 jsonStr+="'"+$(this).text()+"'"+"},{title:";	
	});
	jsonStr=jsonStr.substring(0,jsonStr.length-8);
	jsonStr+="]";
	var json = eval('(' + jsonStr + ')');
	jQuery("#list-name-for-select").bigAutocomplete({
	width:185,
	data:json,
	callback:function(data){
		 //alert("option[text='"+data.title+"']");	 
		 $("#"+name).find('option').each(function(){
			 if($(this).text()==data.title){
				 //alert($(this).val());
				 var id=$(this).val();
				 //getReserviorById(id);
				 getRecordByCode(action,id);
			 }			 
		 });		
	}
	});
}

//form需要提交的表单id,action是后台处理删除此记录的方法
function delRecordByCode(form,action,code){
	var value=$.trim($("#"+code).val());
	//删除非保存的数据
	if($('#saveInf').css("display")=="none"){
		if(value==null || value==""){
			ui.failed("没有删除的记录！");
			return;
		}
	}
	setIsDisable(code,false);
	ui.confirm({
        tip:"是否删除该记录？",
        dClass:"del-record-dialog",
        yes:function(){
        	closeDialog($(".del-record-dialog"));
        	document.getElementById(form).action=action;
        	document.getElementById(form).submit(); 	
        }
    });	
}

//获取所有渠道名字及编号，填充到selectId中
//GetAllCanalName.action
/*
 * @paras: action:获取下拉单的aciton,selectId为填充的下拉单id,action1、action2以及name为被回调函数的参数
 * */
function getSelectContent(action,selectId,action1,action2,name){
	$.ajax({    			
	    url:action,
		type:'POST',
		data:{},
		dataType:'json',
		success:function(data){
			var jsonData = eval("(" + data + ")");
			if(null!=jsonData && jsonData.length>0){
				var obj = document.getElementById(selectId); 
				for(var i=0;i<jsonData.length;++i){
					obj.options.add(new Option(jsonData[i].name,jsonData[i].id));
				}
				//getInitData("GetInitDataOfWaterGate.action", "GetIrrBAWaterGateById.action", "watergateName");
				getInitData(action1, action2, name);
			}
        }
  });
}

function uperCanalAutoDisable(obj){
    var val=$.trim($(obj).val());
	if(val=="1"){	
	//两种方法都可以
	$("#uperCanalCd option:eq(0)").attr('selected','selected');
	$("#uperCanalCd").attr("disabled",true);
	//document.getElementById("uperCanalCd").disabled=true;
	}
	else{
		$("#uperCanalCd").attr("disabled",false);
	}
}